package de.tsystems.dict.webservice.impl;

import de.tsystems.dict.api.LanguageService;
import de.tsystems.dict.model.Language;
import de.tsystems.dict.webservice.LanguageManipulationService;

import javax.inject.Inject;
import javax.inject.Named;
import java.util.List;

@Named("languageManipulationService")
class LanguageManipulationServiceImpl implements LanguageManipulationService {

    @Inject
    @Named("languageService")
    private LanguageService languageService;

    public LanguageService getLanguageService() {
        return languageService;
    }

    public void setLanguageService(LanguageService languageService) {
        this.languageService = languageService;
    }

    @Override
    public String greetUser() {
        return "Hello";
    }

    @Override
    public String size() {
        return String.format("%s", languageService.listAll().size());
    }

    @Override
    public String create(LanguageInfo info) {

        if (null == info.getId()) {
            if (null == info.getName()) return "Language must have a name";
            if (languageService.findLanguage(info.getName()) != null) {
                return "Language already exists";
            }
        } else {
            Language language = languageService.getById(info.getId());
            language.setName(info.getName());
            languageService.save(language);
        }

        return "Ok";
    }

    @Override
    public List<Language> list() {
        return languageService.listAll();
    }

}


